﻿using EasyAbp.Abp.PhoneNumberLogin.Provider.Aliyun.Settings;
using System.Threading.Tasks;
using Volo.Abp.Settings;
using Volo.Abp.Sms;
namespace EasyAbp.Abp.PhoneNumberLogin.Provider.Aliyun
{
    public class AliyunPhoneNumberLoginVerificationCodeSender : IPhoneNumberLoginVerificationCodeSender
    {
        private readonly ISmsSender _smsSender;

        private readonly ISettingProvider _settingProvider;

        public AliyunPhoneNumberLoginVerificationCodeSender(
            ISmsSender smsSender,
            ISettingProvider settingProvider)
        {
            _smsSender = smsSender;
            _settingProvider = settingProvider;
        }

        public virtual async Task<bool> SendAsync(string phoneNumber, string code, string signName,string templateId, object textTemplateModel = null)
        {
           
            var smsMessage = new SmsMessage(phoneNumber, PhoneNumberLoginProviderAliyunConsts.PlaceHolder);

            smsMessage.Properties.Add(PhoneNumberLoginProviderAliyunConsts.TemplateCode, templateId);

            smsMessage.Properties.Add(PhoneNumberLoginProviderAliyunConsts.TemplateParam, new { code=code });

            smsMessage.Properties.Add(PhoneNumberLoginProviderAliyunConsts.SignName, signName);

            await _smsSender.SendAsync(smsMessage);

            return true;
        }

        public async Task<bool> SendAsync(string phoneNumber, string code, VerificationCodeType type, object textTemplateModel = null)
        {
            string templateId = string.Empty;

            switch (type)
            {
                case VerificationCodeType.Login:
                    templateId = await _settingProvider.GetOrNullAsync(PhoneNumberLoginProviderAliyunSettings.LoginTemplateCode);
                    break;
                case VerificationCodeType.Register:
                    templateId = await _settingProvider.GetOrNullAsync(PhoneNumberLoginProviderAliyunSettings.RegisterTemplateCode);
                    break;
                case VerificationCodeType.Confirm:
                    templateId = await _settingProvider.GetOrNullAsync(PhoneNumberLoginProviderAliyunSettings.ConfirmTemplateCode);
                    break;
                case VerificationCodeType.ResetPassword:
                    templateId = await _settingProvider.GetOrNullAsync(PhoneNumberLoginProviderAliyunSettings.ResetPasswordTemplateCode);
                    break;
            }
            var smsMessage = new SmsMessage(phoneNumber, "PlaceHolder");


            smsMessage.Properties.Add(PhoneNumberLoginProviderAliyunConsts.TemplateCode, templateId);

            smsMessage.Properties.Add(PhoneNumberLoginProviderAliyunConsts.TemplateParam, new { code=code });

            smsMessage.Properties.Add(PhoneNumberLoginProviderAliyunConsts.SignName, 
                await _settingProvider.GetOrNullAsync(PhoneNumberLoginProviderAliyunSettings.DefaultSignName));

            await _smsSender.SendAsync(smsMessage);

            return true;
        }
    }
}
